package com.wm.action;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.alibaba.fastjson.JSON;
import com.wm.bean.Comment;
import com.wm.service.CommentService;
import com.wm.vo.CommentListVO;
import com.wm.vo.RestaurantListVO;

public class CommentAction extends DispatchAction{
	@Resource private CommentService commentService;

	public ActionForward wwqqfdaafafdawww (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		Comment comment = new Comment();
		this.commentService.listComment(comment);
		return mapping.findForward("listfd");
		
		
	}

	public ActionForward listComment (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		Comment comment = new Comment();
		this.commentService.listComment(comment);
		return mapping.findForward("listfd");
		
		
	}
	
	/**
	 * 插入评论
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

	public void submitComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

		System.out.println("commentcaction");
		
		
		/*SAXReader reader = new SAXReader(); 
		Reader ioread = new InputStreamReader(new FileInputStream("d://test.xml"), "GB2312"); 
		Document document = reader.read(ioread); */




//		request.setCharacterEncoding("gbk");
		HttpSession session = request.getSession();
		
		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式 
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

		 String content = URLDecoder.decode(request.getParameter("comment"),"utf8");
		 String name = request.getParameter("name");
		 
		 System.out.println("comm="+ content);
		 String rid = null ;
		 String userid = null;
		 String user = (String)session.getAttribute("user");
		 
		Comment comment = new Comment();
		comment.setContent(content);
		comment.setDate(df.format(new Date()));
		comment.setRid(rid);
		comment.setUserID(userid);
		
		commentService.save(comment);
		
		//返回前台，显示评论
		response.setContentType("text/xml;charset=UTF-8");  
		 response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		//String output="<DIV class=review_con><DIV class=review_con_left><IMG src='点评-饭易得杭州外_files/s_nopic.jpg' width=54 height=54> </DIV><DIV class=review_con_right><H4><SPAN>sjft</SPAN></H4><P>真心建议你们扩展一下业务呀范围，增加外送费用，这样让我们这些住得比较偏远的没东西可吃，就太郁闷了</P><P class=f12>2013-5-17 15:54:42发布</P></DIV></DIV>";   //处理接收到的参数，生成响应的XML文档
		String output = "<div class=\"review_con\"><div class=\"review_con_left\"><img src=\"images/s_nopic.jpg\" width=\"54\" height=\"54\" /></div><div class=\"review_con_right\"><h4><span>"+user+"</span></h4>" +
				" <p> "+content+   "</p><p class=\"f12\">"+df.format(new Date())+"发布</p></div></div>";
		
		
//		String output="<div>用户名已存在</div>";  

		 returnValue.print(output);   
		 returnValue.close();   
		
	}
	
	/**
	 * 初始化首页 评论展示
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */

/*	public void initComment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String uid = request.getParameter("uid");
		
		
		CommentListVO commentListVO = commentService.getCommentByHql(commentListVO);
		String commentlistJson = JSON.toJSONString(commentListVO); 
		System.out.println(commentlistJson);
		
		//返回前台，显示评论
		response.setContentType("text/xml;charset=UTF-8");  
		response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		String output = commentlistJson;
		
		 returnValue.print(output);   
		 returnValue.close();   
		
	}*/
	
	/**
	 * 展示评论
	 * @throws UnsupportedEncodingException 
	 * 
	 */
		
		public ActionForward InitialComment(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
			
			int page = 1;
			System.out.println(request.getParameter("page"));
			if (request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
				page = Integer.parseInt(request.getParameter("page"));
			}
			 CommentListVO commentListVO = new CommentListVO();
			commentListVO.requestPage = page;
			String keyWord = request.getParameter("keyword");
			commentListVO.keyWord  = java.net.URLDecoder.decode(keyWord,"UTF-8");
			request.setAttribute("comment", commentService.getCommentByHql(commentListVO));
//			System.out.println(searchService.SearchRest(restaurantListVO).restaurantsList.size());
			return mapping.findForward("commentResult");
		}
	/***
	 * 
	* 取得所有的评论数据
	* 
	*/
	public void getAllComments(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
		System.out.println("------getallCommentsacton");
		int page = 1;
//		System.out.println("请求第" + request.getParameter("page") + "页");
		if (request.getParameter("page") != null
				&& !request.getParameter("page").equals("")) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		CommentListVO commentListVO = new CommentListVO();
		commentListVO.requestPage = page;
		
		commentListVO= commentService.listComments(commentListVO);
		
		String commentlistJson = JSON.toJSONString(commentListVO);
		response.setContentType("text/xml;charset=UTF-8");  
		response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		String output = commentlistJson;
		
		returnValue.print(output);   
		returnValue.close();  
		
		//return mapping.findForward("comment");

	}
	
	public ActionForward submi(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{

		System.out.println("commentcaction");
		
		
		/*SAXReader reader = new SAXReader(); 
		Reader ioread = new InputStreamReader(new FileInputStream("d://test.xml"), "GB2312"); 
		Document document = reader.read(ioread); */




//		request.setCharacterEncoding("gbk");
		HttpSession session = request.getSession();
		
		 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式 
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

		 String content = URLDecoder.decode(request.getParameter("comment"),"utf8");
		 String name = request.getParameter("name");
		 
		 System.out.println("comm="+ content);
		 String rid = null ;
		 String userid = null;
		 String user = (String)session.getAttribute("user");
		 
		Comment comment = new Comment();
		comment.setContent(content);
		comment.setDate(df.format(new Date()));
		comment.setRid(rid);
		comment.setUserID(userid);
		
		commentService.save(comment);
		
		//返回前台，显示评论
		response.setContentType("text/xml;charset=UTF-8");  
		 response.setHeader("Cache-Control", "no-cache");   
		PrintWriter returnValue=response.getWriter();   
		//String output="<DIV class=review_con><DIV class=review_con_left><IMG src='点评-饭易得杭州外_files/s_nopic.jpg' width=54 height=54> </DIV><DIV class=review_con_right><H4><SPAN>sjft</SPAN></H4><P>真心建议你们扩展一下业务呀范围，增加外送费用，这样让我们这些住得比较偏远的没东西可吃，就太郁闷了</P><P class=f12>2013-5-17 15:54:42发布</P></DIV></DIV>";   //处理接收到的参数，生成响应的XML文档
		String output = "<div class=\"review_con\"><div class=\"review_con_left\"><img src=\"images/s_nopic.jpg\" width=\"54\" height=\"54\" /></div><div class=\"review_con_right\"><h4><span>"+user+"</span></h4>" +
				" <p> "+content+   "</p><p class=\"f12\">"+df.format(new Date())+"发布</p></div></div>";
		
		
//		String output="<div>用户名已存在</div>";  

		 returnValue.print(output);   
		 returnValue.close();   
		 return mapping.findForward("commentResult");
	}
}
